<template>
  <ContainerGroup>
    <Container style="margin-bottom: 10px">
      <FilterForm
        ref="filterForm"
        :elements="elements"
        noimmediate
        @submit="handleSearch"
        @reset="handleReset"
      >
        <Button
          icon="el-icon-refresh-right"
          :btn-loading="dcLoading"
          @click="handleStart"
        >开始采集</Button>
      </FilterForm>
    </Container>
    <Container height="fill">
      <div class="wraper-title">采集记录</div>
      <div class="wraper">
        <Table
          :columns="tableColumns"
          :loading="tableLoading"
          :data="tableData"
          :pagination="{
            total: total,
            current: currentPage,
            pageSize: pageSize,
          }"
          @paginationChange="pageChange"
          @rowSelection="handleSelection"
        />
      </div>
    </Container>
  </ContainerGroup>
</template>
<script>
import {
  getAssessmentCyclePull
} from '@/api/weight'
import {
  dataDecimation
} from '@/api/dataCleaning'
import {
  getAssessmentCycleById
} from '@/api/targetManagement'
export default {
  data() {
    return {
      dcLoading: false,
      tableColumns: [
        {
          title: '操作时间',
          dataIndex: ''
        },
        {
          title: '采集月份',
          dataIndex: ''
        },
        {
          title: '状态',
          dataIndex: ''
        },
        {
          title: '操作人',
          dataIndex: ''
        }
      ],
      total: 0,
      pageSize: 100,
      currentPage: 1,
      tableLoading: false,
      tableData: [],
      newCondition: {},
      elements: [
        {
          key: 'cycle',
          label: '周期',
          type: 'select',
          itemLabel: 'periodPull',
          itemValue: 'id',
          hideSelectAll: true,
          options: [],
          value: ''
        }
      ],
      imageShow: false
    }
  },
  created() {
    this.getSelect()
  },
  methods: {
    getFileInfo() {
      const id = this.$refs.filterForm.getParms()?.cycle
      getAssessmentCycleById({
        id: id
      }).then((res) => {
        if (res.code === 200) {
          const { isFile } = res.data
          if (isFile === 1) {
            this.imageShow = true
          } else {
            this.imageShow = false
          }
        }
      })
    },
    getSelect() {
      // 周期
      getAssessmentCyclePull().then((res) => {
        if (res.code === 200) {
          this.elements[0].options = res.data
          const { id } = res.data[0]
          this.elements[0].value = id
          localStorage.setItem(
            'cycle',
            JSON.stringify(this.elements[0].options)
          )
          this.handleSearch(this.$refs.filterForm.getParms())
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 开始采集
    handleStart() {
      this.dcLoading = true
      const params = {
        id: this.$refs.filterForm.getParms()?.cycle,
        period: this.elements[0].options.find(item => item.id === this.$refs.filterForm.getParms()?.cycle)?.periodSuffix
      }
      dataDecimation(params)
        .then((res) => {
          this.dcLoading = false
          if (res.code === 200) {
            this.$message.success(res.data)
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(() => {
          this.dcLoading = false
        })
    },
    // 查询
    handleSearch(value) {
      this.newCondition = value
      this.currentPage = 1
      this.getData()
      this.getFileInfo()
    },
    // 重置
    handleReset(val) {
      this.handleSearch(val)
    },
    // 数据
    getData() {
      // const params = {
      //   pageNum: this.currentPage,
      //   pageSize: this.pageSize,
      //   assessmentCycleId: this.newCondition.cycle,
      //   accountingCode: this.newCondition.accountingUnit
      //   // type: this.newCondition.type,
      // }
      // this.tableLoading = true
      // listAccountingUnitWeightByPage(params)
      //   .then((res) => {
      //     this.tableLoading = false
      //     if (res.code === 200) {
      //       this.tableData = res.data.records
      //       this.total = res.data.total
      //     } else {
      //       this.$message.error(res.msg)
      //     }
      //   })
      //   .catch(() => {
      //     this.tableLoading = false
      //   })
    },
    // 分页
    pageChange(payload) {
      this.currentPage = payload.current
      this.pageSize = payload.pageSize
      this.getData()
    }
  }
}
</script>
<style lang="less" scoped>
.wraper {
  display: flex;
  height: calc(100% - 30px);
  overflow: hidden;
  .upload-demo {
    .buttons {
      margin: 0px 5px;
      height: 27px;
      margin-top: -1px;
    }
  }
}
.wraper-title {
  font-size: 13px;
  font-weight: bold;
  margin-bottom: 10px;
}
</style>
